// 我的我的/housekeeping_form/housekeeping_form.js
Page({
  data: {
    selectedDate: '',
    timeSlotOptions: ['08:00-10:00', '10:00-12:00', '12:00-14:00', '14:00-16:00', '16:00-18:00', '18:00-20:00'],
    selectedTimeSlotIndex: null,
    selectedService: 'window', // 默认擦玻璃
    selectedGender: 'none', // 默认不限
    contactName: '',
    phoneNumber: '',
    address: '',
    note: ''
  },

  onLoad: function() {
    // 设置默认日期为今天
    const today = new Date();
    const year = today.getFullYear();
    const month = (today.getMonth() + 1).toString().padStart(2, '0');
    const day = today.getDate().toString().padStart(2, '0');
    this.setData({
      selectedDate: `${year}-${month}-${day}`
    });
  },

  // 日期选择器变化
  bindDateChange: function(e) {
    this.setData({
      selectedDate: e.detail.value
    });
  },

  // 时间段选择器变化
  bindTimeSlotChange: function(e) {
    this.setData({
      selectedTimeSlotIndex: e.detail.value
    });
  },

  // 选择服务类型
  selectService: function(e) {
    const service = e.currentTarget.dataset.service;
    this.setData({
      selectedService: service
    });
  },

  // 选择服务人员性别
  selectGender: function(e) {
    const gender = e.currentTarget.dataset.gender;
    this.setData({
      selectedGender: gender
    });
  },

  // 输入联系人姓名
  inputContactName: function(e) {
    this.setData({
      contactName: e.detail.value
    });
  },

  // 输入手机号码
  inputPhoneNumber: function(e) {
    this.setData({
      phoneNumber: e.detail.value
    });
  },

  // 输入家庭住址
  inputAddress: function(e) {
    this.setData({
      address: e.detail.value
    });
  },

  // 输入备注要求
  inputNote: function(e) {
    this.setData({
      note: e.detail.value
    });
  },

  // 提交表单
  submitForm: function() {
    const { selectedDate, selectedTimeSlotIndex, selectedService, selectedGender, contactName, phoneNumber, address } = this.data;
    const note = this.data.note || '无特殊要求';
    
    // 验证必填项
    if (!contactName) {
      wx.showToast({
        title: '请输入联系人姓名',
        icon: 'none'
      });
      return;
    }
    
    if (!phoneNumber) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }

    if (!address) {
      wx.showToast({
        title: '请输入家庭住址',
        icon: 'none'
      });
      return;
    }

    // 验证日期
    if (!selectedDate) {
      wx.showToast({
        title: '请选择日期',
        icon: 'none'
      });
      return;
    }

    // 验证时间段
    if (selectedTimeSlotIndex === null) {
      wx.showToast({
        title: '请选择时间段',
        icon: 'none'
      });
      return;
    }

    // 格式化日期显示
    const dateDisplay = selectedDate;

    // 格式化时间显示
    const timeDisplay = this.data.timeSlotOptions[selectedTimeSlotIndex];

    // 格式化服务类型显示
    let serviceDisplay = '擦玻璃';
    if (selectedService === 'kitchen') serviceDisplay = '厨房清洁';
    if (selectedService === 'bathroom') serviceDisplay = '卫生间清洁';
    if (selectedService === 'whole') serviceDisplay = '全屋清洁';

    // 格式化性别显示
    let genderDisplay = '不限';
    if (selectedGender === 'male') genderDisplay = '男';
    if (selectedGender === 'female') genderDisplay = '女';

    const appointmentInfo = {
      date: dateDisplay,
      time: timeDisplay,
      service: serviceDisplay,
      gender: genderDisplay,
      contact: contactName,
      phone: phoneNumber,
      address: address,
      note: note
    };
    
    // 这里可以添加提交到服务器的逻辑
    wx.showModal({
      title: '预约成功',
      content: `您已成功预约${appointmentInfo.service}服务，时间：${appointmentInfo.date} ${appointmentInfo.time}，服务人员性别：${appointmentInfo.gender}，联系人：${appointmentInfo.contact}，电话：${appointmentInfo.phone}，地址：${appointmentInfo.address}，备注：${appointmentInfo.note}`,
      showCancel: false,
      success: (res) => {
        if (res.confirm) {
          wx.navigateBack();
        }
      }
    });
  }
});